package com.lcx.movie.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcx.movie.common.Rep;
import com.lcx.movie.entity.MovieFavorites;
import com.lcx.movie.service.MovieFavoritesService;
import net.minidev.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 李丽君
 * @version 1.0
 * @Description
 * @date 2025/2/27 10:29
 */
@RestController
@CrossOrigin(origins = "*")
public class MovieFavoritesController {
    @Autowired
    private MovieFavoritesService movieFavoritesService;
    /**
     * 前台新增我的收藏电影
     * @param movieFavorites 电影收藏实体
     * @return 受影响的行数
     */
    @PostMapping("/insertMovieFavorites")
    public String insertMovieFavorites(@RequestBody MovieFavorites movieFavorites){
        int insertMovieFavorites = movieFavoritesService.insertMovieFavorites(movieFavorites);
        if (insertMovieFavorites > 0){
            return "新增收藏成功";
        }else {
            return "新增收藏失败";
        }
    }
    /**
     * 前台根据用户id查询我的收藏电影
     * @param userId 用户id
     * @return 收藏的电影集合
     */
    @GetMapping("/selectMovieFavoritesList")
    public Rep selectMovieFavoritesList(@Param("userId") Integer userId){
        List<MovieFavorites> movieFavorites = movieFavoritesService.selectMovieFavoritesList(userId);
        return new Rep().addData("movieFavorites", movieFavorites);
    }
    /**
     * 前台根据收藏的电影id取消收藏电影
     * @param movieId 电影id
     * @return 受影响的行数
     */
    @GetMapping("/deleteFavorite")
    public String deleteFavorite(@Param("movieId") Integer movieId){
        int deleteFavorite = movieFavoritesService.deleteFavorite(movieId);
        if (deleteFavorite > 0){
            return "取消收藏成功";
        }else {
            return "取消收藏失败";
        }
    }
    /**
     * 前台  收藏前判断该用户此影片是否已经收藏过
     * @param userId 用户id
     * @param movieId 电影id
     * @return 受影响的行数
     */
    @GetMapping("/checkMovieFavorite")
    public String checkMovieFavorite(@Param("userId") Integer userId, @Param("movieId") Integer movieId){
        int checkMovieFavorite = movieFavoritesService.checkMovieFavorite(userId, movieId);
        if (checkMovieFavorite > 0){
            return "exist";
        }else{
            return "not exist";
        }
    }
    /**
     * 主页面的最受期待
     * @return 最受期待的电影集合
     */
    @GetMapping("/mostExpectedMovies")
    public JSONObject mostExpectedMovies(){
        JSONObject obj=new JSONObject();
        List<MovieFavorites> mostedExpectedMovies = movieFavoritesService.mostExpectedMovies();
        obj.put("data", mostedExpectedMovies);
        obj.put("count", mostedExpectedMovies.size());
        return obj;
    }

    /**
     * 根据用户名或者影片名查询收藏的电影
     * @param userName 用户名
     * @param movieCnName 电影名
     * @return 收藏电影集合
     */
    @GetMapping("/selectMovieFavorites")
    public Rep selectMovieFavorites(@Param("page") Integer page,@Param("pageSize") Integer pageSize
            ,@Param("userName")String userName,@Param("movieCnName")String movieCnName){
        // 开启分页
        PageHelper.startPage(page,pageSize);
        // 执行查询
        List<MovieFavorites> movieFavorites = this.movieFavoritesService.selectMovieFavorites(userName, movieCnName);
        // 创建分页对象
        PageInfo<MovieFavorites> movieFavoritesPageInfo=new PageInfo<>(movieFavorites);
        return new Rep().addData("movieFavorites", movieFavoritesPageInfo);
    }
}
